﻿/*--------------------------------------------------------------------------------
 * Adaptive Intelligence - Windows Forms Controls Class Library
 * Version 2.00
 * 
 * Toolbox Entry Panel Class
 *      Provides a UI element for containing a set of ToolboxEntryControl instances.
 *------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------
 * System Imports
 *------------------------------------------------------------------------------*/
using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace Adaptive.Foundation.Windows.Forms
{
    /// <summary>
    /// Provides a UI element for containing a set of <see cref="ToolboxEntryControl"/> instances.
    /// </summary>
    [ToolboxItem(true)]
    public partial class ToolboxEntryPanel : ToolboxControlBase
    {
        /*--------------------------------------------------------------------------------
         * Private Member Declarations
         *------------------------------------------------------------------------------*/
        #region Private Member Declarations
        /// <summary>
        /// Collapsed or expanded flag.
        /// </summary>
        private bool _collapsed;
        #endregion

        /*--------------------------------------------------------------------------------
         * Constructor / Dispose / Destructor Methods
         *------------------------------------------------------------------------------*/
        #region Constructor / Dispose / Destructor Methods
        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        /// <remarks>
        /// This is the default constructor.
        /// </remarks>
        public ToolboxEntryPanel()
            : base()
        {
            InitializeComponent();
        }
        /// <summary>
        /// Deallocates internally allocated objects.
        /// </summary>
        /// <param name="disposing">
        /// A value indicating whether the object is being disposed.
        /// </param>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                Controls.Clear();
                if (components != null)
                    components.Dispose();
            }
            components = null;
            base.Dispose(disposing);
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Protected Methods / Event Methods
         *------------------------------------------------------------------------------*/
        #region Protected Methods / Event Methods
        /// <summary>
        /// Forces the control to re-draw when the colors are changed.
        /// </summary>
        protected override void ResetColors()
        {
            //Copy color values to controls.
            foreach (ToolboxEntryControl item in Controls)
            {
                item.ItemHighlightBackColor = ItemHighlightBackColor;
                item.ItemHighlightForeColor = ItemHighlightForeColor;
                item.ItemForeColor = ItemForeColor;
                item.ItemBackColor = ItemBackColor;
                item.TextIndentation = TextIndentation;
            }

            //Invoke base method.
            base.ResetColors();
        }
        /// <summary>
        /// Raises the <see cref="E:System.Windows.Forms.Control.Resize"/> event and sets the appropriate height of the control.
        /// </summary>
        /// <param name="e">An <see cref="T:System.EventArgs"/> that contains the event data.</param>
        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            if (_collapsed)
                Height = 0;
            else
            {
                Height = Controls.Count * 15;
            }
        }
        /// <summary>
        /// Raises the <see cref="E:System.Windows.Forms.Control.Paint"/> event.
        /// </summary>
        /// <param name="e">A <see cref="T:System.Windows.Forms.PaintEventArgs"/> that contains the event data.</param>
        protected override void OnPaint(PaintEventArgs e)
        {
            //Set the control height.
            SetSize();

            if ((Visible) && (Height > 0))
            {
                foreach (ToolboxEntryControl control in Controls)
                    control.Refresh();
            }
            base.OnPaint(e);
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Public Methods / Functions
         *------------------------------------------------------------------------------*/
        #region Public Methods / Functions
        /// <summary>
        /// Adds a new toolbox entry item to the panel.
        /// </summary>
        /// <param name="newItem">The display text for the new item.</param>
        public void AddItem(string newItem)
        {
            ToolboxEntryControl newPanel = new ToolboxEntryControl();
            newPanel.Name = Name + newItem.Replace(" ", "_");
            newPanel.ReferenceData = newItem;

            SetDefaults(newPanel);
            Invalidate();
        }
        /// <summary>
        /// Adds a new toolbox entry item to the panel.
        /// </summary>
        /// <param name="newItem">
        /// An <see cref="INamedInstance"/>-derived object instance to be added to the 
        /// toolbox.  The value of the <see cref="INamedInstance.Name">Name</see> property 
        /// is used as the display text.
        /// </param>
        public void AddItem(INamedInstance newItem)
        {
            ToolboxEntryControl newPanel = new ToolboxEntryControl();
            newPanel.Name = Name + newItem.Name.Replace(" ", "_");
            newPanel.ReferenceData = newItem;

            SetDefaults(newPanel);
            Invalidate();
        }
        /// <summary>
        /// Adds a new toolbox entry item to the panel.
        /// </summary>
        /// <param name="newItem">
        /// A <see cref="NamedDisposableObjectBase"/>-derived object instance to be added to the 
        /// toolbox.  The value of the <see cref="NamedDisposableObjectBase.Name">Name</see> property 
        /// is used as the display text.
        /// </param>
        public void AddItem(NamedDisposableObjectBase newItem)
        {
            ToolboxEntryControl newPanel = new ToolboxEntryControl();
            newPanel.Name = Name + newItem.Name.Replace(" ", "_");
            newPanel.ReferenceData = newItem;

            SetDefaults(newPanel);
            Invalidate();
        }
        /// <summary>
        /// Adds a new toolbox entry item to the panel.
        /// </summary>
        /// <param name="newItem">
        /// A <see cref="Control"/>-derived object instance to be added to the 
        /// toolbox.  The value of the <see cref="Control.Text">Text</see> property 
        /// is used as the display text if populated, otherwise, the 
        /// value of the <see cref="Control.Name">Name</see> property  is used.
        /// </param>
        public void AddItem(Control newItem)
        {
            ToolboxEntryControl newPanel = new ToolboxEntryControl();
            newPanel.Name = Name + newItem.Name.Replace(" ", "_");
            newPanel.ReferenceData = newItem;

            SetDefaults(newPanel);
            Invalidate();
        }
        /// <summary>
        /// Collapses this instance of the panel.
        /// </summary>
        public void Collapse()
        {
            _collapsed = true;
            Height = 0;
            Invalidate();
        }
        /// <summary>
        /// Expands this instance of the panel.
        /// </summary>
        public void Expand()
        {
            _collapsed = false;
            Height = 1;
            Height = 0;
            Invalidate();
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Private Methods / Functions
         *------------------------------------------------------------------------------*/
        #region Private Methods / Functions
        /// <summary>
        /// Sets the default property values for new items as they are added.
        /// </summary>
        /// <param name="controlItem">
        /// The <see cref="ToolboxEntryControl"/> instance to be modified.
        /// </param>
        private void SetDefaults(ToolboxEntryControl controlItem)
        {
            controlItem.Height = 15;
            controlItem.ForeColor = ForeColor;
            controlItem.BackColor = BackColor;
            controlItem.ItemBorderColor = ItemBorderColor;
            controlItem.ItemHighlightBackColor = ItemHighlightBackColor;
            controlItem.ItemHighlightForeColor = ItemHighlightForeColor;
            controlItem.ItemForeColor = ItemForeColor;
            controlItem.ItemBackColor = ItemBackColor;
            controlItem.TextIndentation = TextIndentation;
            controlItem.RightClick +=new EventHandler(HandleControlRightClick);
            Controls.Add(controlItem);
            controlItem.Dock = DockStyle.Top;
            controlItem.Visible = true;
            controlItem.BringToFront();
        }
        private void SetSize()
        {
            if (_collapsed)
            {
                if (Height != 0)
                    Height = 0;
            }
            else
            {
                if ((Controls.Count > 0) && (Height != Controls.Count * 15))
                    Height = (Controls.Count * 15);
            }
        }
        /// <summary>
        /// Handles the event when a sub-control is right-clicked.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void HandleControlRightClick(object sender, EventArgs e)
        {
            OnRightClick(e);
        }
        #endregion

    }
}
